<? include block('htmla') ?>

<? startScript('jquery, jquery.tablednd'); ?><script type="text/javascript">

jQuery(function($) {

	process = function(e, data) {
		switch(e) {
			case "embed-url":	// skipping intro by default
				break;
			case "unembed-url":	// skip if 2 or more langs, else kill intro record
				break;
			case "skip-intro":	// make intro redirect
				break;
			case "unskip-intro":	// make intro the default page
				break;
		}
	}
					 
	$("#objects").tableDnD({
	    onDragClass: "dragging",
	    onDrop: function(table, row) {
			$.post("sort-manual/", {order:$(table).tableDnDSerialize()});
	    },
		dragHandle: "dragHandle"
	});
	
	$("#objects .object").hover(function() {
          $(this).addClass('showDragHandle');
	    }, function() {
          $(this).removeClass('showDragHandle');
	    }
	);
	
	$("#objects .object a.button").click(function() {
		var self = $(this), href = self.attr("href"), parent = self.parents(".object"), id = parent.attr("id").substr(4), title = self.attr("title").match(/«(.*)»/)[1], attr = ~~parent.attr("data-attr");
		
		if (self.hasClass('visible')) {
			if (!(attr & <?=LL_DEFAULT?>)) {
				$.post(href);
				self.removeClass('visible').addClass('hidden').
					attr("title", "<?=t('Enable «!name»', 'tpl')?> ".replace("!name", title)).
					attr("href", href.replace("disable", "enable"));
				parent.attr("data-attr", attr & ~<?=LL_ACTIVE?>);
			}
			return false;
		} else

		if (self.hasClass('hidden')) {
			$.post(href);
			self.removeClass('hidden').addClass('visible').
				attr("title", "<?=t('Disable «!name»', 'tpl')?>".replace("!name", title)).
				attr("href", href.replace("enable", "disable"));
			parent.attr("data-attr", attr | <?=LL_ACTIVE?>);
			return false;
		} else

		if (self.hasClass('staroff')) {
			if (attr & <?=LL_ACTIVE?>) {
				$.post(href);
				
				$("#objects .object a.button.staron").each(function(){
					var self = $(this), title = self.attr("title").match(/«(.*)»/)[1],
						parent = self.parents(".object"), attr = ~~parent.attr("data-attr");
						
					self.removeClass('staron').addClass('staroff').
						attr("title", "<?=t('Make «!name» the default site language', 'tpl')?>".replace("!name", title));
						
					parent.attr("data-attr", attr & ~<?=LL_DEFAULT?>);
				});
				
				self.removeClass('staroff').addClass('staron').
					attr("title", "<?=t('«!name» is the default site language', 'tpl')?>".replace("!name", title));
					
				parent.attr("data-attr", attr | <?=LL_DEFAULT?>);
			}
			return false;
		} else

		if (self.hasClass('staron')) { return false } else

		if (self.hasClass('admstaroff')) {
			$.post(href);
			
			$("#objects .object a.button.admstaron").each(function(){
				var self = $(this), title = self.attr("title").match(/«(.*)»/)[1],
					parent = self.parents(".object"), attr = ~~parent.attr("data-attr");
					
				self.removeClass('admstaron').addClass('admstaroff').
					attr("title", "Make «!name» the default admin language".replace("!name", title));
					
				parent.attr("data-attr", attr & ~<?=LL_ADMIN?>);
			});
			
			self.removeClass('admstaroff').addClass('admstaron').
				attr("title", "«!name» is the default admin language".replace("!name", title));
			return false;
		} else

		if (self.hasClass('admstaron')) { return false } else

		if (self.hasClass('kill')) {
			if (confirm("<?=ht('Are you sure you want to kill «!name»?', 'tpl')?>".replace("!name", title))) {
				$.post(href);
				parent.remove();
			}
			return false;
		}

		return true;
	});
	
});

</script><? endScript();



?><fieldset><legend> <?=dat('page','name')?> </legend><div>

<? if (sizeof($d = dat('objects'))) { 

	?><table class="adm dnd" id="objects"><thead><tr class="nodrag nodrop">
		<td><?=t('Edit', 'tpl')?></td><td></td><td><?=t('Language code (RFC 3066)', 'tpl')?></td><td><?=t('Language name', 'tpl')?></td>
	</tr></thead><tbody><?

	foreach($d as $a) {
		?><tr id="obj-<?=$a['id']?>" class="object" data-attr="<?=$a['attr']?>">
			<td>
				<? if ($a['attr'] & LL_ACTIVE) { ?>
					<a title="<?=ht('Disable «!name»', 'tpl', array('!name' => $a['text']))?>" class="button visible" href="disable-<?=$a[id]?>/"></a>
				<? } else { ?>
					<a title="<?=ht('Enable «!name»', 'tpl', array('!name' => $a['text']))?>" class="button hidden" href="enable-<?=$a[id]?>/"></a>
				<? } ?>

				<? if ($a['attr'] & LL_DEFAULT) { ?>
					<a title="<?=ht('«!name» is the default site language', 'tpl', array('!name' => $a['text']))?>" class="button staron" href="langdef-<?=$a[id]?>/"></a>
				<? } else { ?>
					<a title="<?=ht('Make «!name» the default site language', 'tpl', array('!name' => $a['text']))?>" class="button staroff" href="langdef-<?=$a[id]?>/"></a>
				<? } ?>

				<? if ($a['attr'] & LL_ADMIN) { ?>
					<a title="<?=ht('«!name» is the default admin language', 'tpl', array('!name' => $a['text']))?>" class="button admstaron" href="admdef-<?=$a[id]?>/"></a>
				<? } else { ?>
					<a title="<?=ht('Make «!name» the default admin language', 'tpl', array('!name' => $a['text']))?>" class="button admstaroff" href="admdef-<?=$a[id]?>/"></a>
				<? } ?>
				
				<a title="<?=ht('Edit «!name»', 'tpl', array('!name' => $a['text']))?>" class="button edit" href="edit-<?=$a['id']?>/"></a>
				
			<? if (!($a['attr'] & LL_CORE)) { ?>
				<a title="<?=ht('Kill «!name»', 'tpl', array('!name' => $a['text']))?>" class="button kill" href="delete-<?=$a['id']?>/"></a>
			<? } ?>
			</td>
			<td class="dragHandle"></td>
			<td><a href="edit-<?=$a[id]?>/" title="<?=ht('Edit «!name»', 'tpl', array('!name' => $a['text']))?>"><?=$a['text']?></a></td>
			<td><a href="edit-<?=$a[id]?>/" title="<?=ht('Edit «!name»', 'tpl', array('!name' => $a['text']))?>"><?=$a['assoc']?></a></td>
		  </tr>
		<?
	}

	?></tbody></table><?

/*
	?><label><input type="checkbox" name="lang_embed" value="1" <?=dat('embed') ? 'checked="checked"' : ''?>/><?=t('Embed language code in url', 'tpl')?></label><br /><?
	?><label><input type="checkbox" name="skip_intro" value="1" <?=dat('skip') ? 'checked="checked"' : ''?>/><?=t('Skip intro page', 'tpl')?></label><?
*/

} else {

	?><?=t('No languages', 'tpl')?><?

} ?>

<h3><?=t('Add language', 'tpl')?></h3>
<form action="" method="post">
	<label><?=t('Language code (RFC 3066)', 'tpl')?> 
		<input type="text" name="title" class="narrow" /></label>
	<input type="submit" name="create_object" value="<?=ht('Add', 'tpl')?>" />
</form>

</fieldset>


<? include block('_htmla');